
package com.wallj.videolibrary.base;

import android.app.Activity;
import android.content.Context;
import android.graphics.SurfaceTexture;
import android.media.AudioManager;
import android.media.MediaMuxer;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;

import android.util.Log;
import android.view.Surface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import com.eyedog.uilibrary.base.BaseActivity;

import com.wallj.videolibrary.player.GLVideoSurfaceView;
import com.wallj.videolibrary.player.renderer.OesRenderer;
import com.wallj.videolibrary.player.renderer.VideoRenderer;
import io.vov.vitamio.LibsChecker;
import io.vov.vitamio.MediaPlayer;
import java.io.IOException;

public abstract class BaseVideoActivity extends BaseActivity {

    private final String TAG = "BaseVideoActivity";
    protected boolean isFinishing = false;
    protected GLVideoSurfaceView mSurfaceView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!LibsChecker.checkVitamioLibs(this)) {
            return;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        isFinishing = false;
        if (mSurfaceView != null) {
            mSurfaceView.onResume();
        }
    }

    public void startPlay(Context context, String filepath) {
        VideoPlayerInstance.getInstance().startPlay(context, filepath);
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mSurfaceView != null) {
            mSurfaceView.onPause();
        }
        if (isFinishing) {
            //release
            VideoPlayerInstance.getInstance().onDestroy();
        } else {
            //pause
            VideoPlayerInstance.getInstance().pause();
        }
    }

    public MediaPlayer getMediaPlayer() {
        return VideoPlayerInstance.getInstance().getMediaPlayer();
    }

    public boolean hasPlayerPrepared() {
        return VideoPlayerInstance.getInstance().hasPlayerPrepared();
    }

    @Override
    public void finish() {
        isFinishing = true;
        super.finish();
    }

    public void onSurfaceViewCreated(final GLVideoSurfaceView surfaceView) {
        mSurfaceView = surfaceView;
        VideoPlayerInstance.getInstance().onSurfaceViewCreated(surfaceView);
    }
}
